﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Configuration;
using IHISCETeim.BLL;

namespace IHISCETeim.SAPSyncSvr
{
    public partial class SyncSvr : ServiceBase
    {
        public SyncSvr()
        {
            InitializeComponent();
           
        }
        /// <summary>
        /// 获取周期时间
        /// </summary>
        /// <returns></returns>
        private int GetSnycInterval()
        {
            int SnycInterval = 0;
            try
            {
                SnycInterval = Convert.ToInt32(ConfigurationManager.AppSettings["SnycInterval"]);
            }
            catch
            {
                SnycInterval = 60;
            }
            return SnycInterval * 1000*60;
        }
   
        protected override void OnStart(string[] args)
        {
            Common.WriteLog("SAP数据同步服务已启动!");
            timer1.Interval = GetSnycInterval(); 
          
            //timer1.Interval = 1000 * 10;
            timer1.Enabled = true;            
            timer1.Start();
            ///
          
        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            timer1.Stop();
            Common.WriteLog("SAP数据同步服务已停止!");
        }


        /// <summary>
        /// 开始同步操作
        /// </summary>
        private void StartSync()
        {
            string SyncResult = "";
            string 表名 = "";


            if (DateTime.Now.Hour == 12 || DateTime.Now.Hour == 18 || DateTime.Now.Hour == 6)
            {
                //1.同步代理商列表
                表名 = "代理商列表";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.SyncAgentList();
                SnycResultWriteLog(SyncResult, 表名);
                ///2.同步索赔件发货记录
                表名 = "索赔件发货记录";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.SyncUClaimDemage();
                SnycResultWriteLog(SyncResult, 表名);

                ///3.同步更新索赔件预计返回日期,终端平台中的旧件管理
                表名 = "更新索赔件预计返回日期";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.UpdateInWillDate();
                SnycResultWriteLog(SyncResult, 表名);

                表名 = "执行索赔费用管控表金额统计程序";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.FixedTimeExecStatistical();
                SnycResultWriteLog(SyncResult, 表名);
            }
            //处理索赔件发货后短信通知 20150421取消
            //if (DateTime.Now.Hour == 12 || DateTime.Now.Hour == 7)
            //{
            //    表名 = "执行索赔发货短信提醒通知程序";
            //    StateWriteLog(表名);
            //    SyncResult = SyncSAPDAL.ProcessUClaimDemagePjSend();
            //    SnycResultWriteLog(SyncResult, 表名);

            //}
            ///以下为生管部物料管控系统同步相关

            //表名 = "更新SAP原材料数据";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncOITM();
            //SnycResultWriteLog(SyncResult, 表名);

            //表名 = "更新SAP收货采购订单";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncOPDN();
            //SnycResultWriteLog(SyncResult, 表名);

            //表名 = "更新SAP库存转储单";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncOWTR();
            //SnycResultWriteLog(SyncResult, 表名);

            //表名 = "更新SAP收货";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncOIGN();
            //SnycResultWriteLog(SyncResult, 表名);

            //表名 = "更新SAP发货";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncOIGE();
            //SnycResultWriteLog(SyncResult, 表名);

            //表名 = "更新SAP退货";
            //StateWriteLog(表名);
            //SyncResult = SyncSAPDAL.SyncORPD();
            //SnycResultWriteLog(SyncResult, 表名);

            if (DateTime.Now.Hour == 5)
            {
                表名 = "同步SAP采购单(模具分摊功能)";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.SyncOPOR();
                SnycResultWriteLog(SyncResult, 表名);
            }
            //2012-10-24 每天　6点　更新应收帐款逾期金额 ,及更新未放款 的到 已放款
            if (DateTime.Now.Hour == 6)
            {
                表名 = "每天更新应收帐款逾期金额(应收帐款)及更新未放款 的到 已放款 及 返回利息状态";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.UpdateFTradeReceivables();
                SnycResultWriteLog(SyncResult, 表名);
            }


            //晚上22点及中捂12点
            if (DateTime.Now.Hour == 22 || DateTime.Now.Hour == 12)
            {
                //同步机器库存表里的最后存放点.2013-04-10
                表名 = "机器库存表同步处理程序(厦门整机库,进品机,及最后库存地)";
                StateWriteLog(表名);
                SyncResult = SyncSAPDAL.SyncOSRI();
                SnycResultWriteLog(SyncResult, 表名);
            }

            //每小时
            if (DateTime.Now.Hour == 8 || DateTime.Now.Hour == 10 || DateTime.Now.Hour == 12 || DateTime.Now.Hour == 14 || DateTime.Now.Hour == 16 || DateTime.Now.Hour == 19)
            {
                表名 = "经销商提前结算新机入库检查费同步处理程序";
                StateWriteLog(表名);
                SyncResult = FTradeWarehouseFeeController.ExecSync()?"":"失败";
                SnycResultWriteLog(SyncResult, 表名);
            }
        }

        private void SnycResultWriteLog(string resultStr, string 表名)
        {
            if (string.IsNullOrEmpty(resultStr))
                SuccessWriteLog(表名, "");
            else
                ErrorWriteLog(表名, "错误消息：" + resultStr);
            EndWriteLog(表名);            
        }

        private void WriteLog(string 标记, string 表名, string 消息)
        {
            Common.WriteLog(string.Format("{0}同步[{1}].{2}",标记,表名,消息));
        }
        private void SuccessWriteLog(string 表名, string 消息)
        {
            WriteLog("成功",表名,消息);
        }
        private void ErrorWriteLog(string 表名, string 消息)
        {
            WriteLog("失败", 表名, 消息);
            //IHISCETeim.BLL.SMSProccessController.AddAppError(string.Format("{0}同步[{1}].{2}", "失败", 表名, 消息));
            IHISCETeim.BLL.SEMailListController.AddAppError("IHISCE同步程序", "IHISCE同步程序", string.Format("{0}同步[{1}].{2}", "失败", 表名, 消息));
        }
        private void StateWriteLog(string 表名)
        {
            WriteLog("开始", 表名,"");
        }
        private void EndWriteLog(string 表名)
        {
            WriteLog("结束", 表名, "");
        }
        bool Exeed = false;
        /// <summary>
        /// 定时执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!Exeed)
            {
                Exeed = true;
                try
                {
                    StartSync();
                }
                catch (System.Exception err)
                {
                    Common.WriteLog(err.Message);
                    IHISCETeim.BLL.SMSProccessController.AddAppError(err.Message);
                }
                Exeed = false;
            }
          
            //Thread t = new Thread(new ThreadStart(StartSync));
           // t.Start();
        }

       

        
    }
}
